package com.server.controller;

import com.server.entity.UserExtendedInformation;
import com.server.entity.UserInfo;
import com.server.entity.UserInformation;
import com.server.service.PhoneCodeService;
import com.server.service.UserService;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Admin
 */
public class UserController {
    /**
     * 用户登录：手机号登录/用户名登录
     *
     * @param usernamePhone 用户名/用户手机号登录
     * @param password      用户密码
     * @param isPhoneLogin  选择登录的方式
     * @return 成功返回用户id，失败返回用户的 -1
     */
    public String phoneLogin(String usernamePhone, String password, boolean isPhoneLogin) {
        UserInformation user = new UserInformation();
        if (isPhoneLogin) {
            if ((usernamePhone.length() < 4) || (usernamePhone.length() > 11) || (password.length() < 4) || (password.length() > 11)) {
                System.out.println("参数错误,请重新输入");
            } else {
                user.setUserName(usernamePhone);
                user.setUserPassword(password);
            }
        } else {
            if ((usernamePhone.length() != 11) || (password.length() < 4) || (password.length() > 11)) {
                System.out.println("参数错误,请重新输入");
            } else {
                user.setUserPhone(usernamePhone);
                user.setUserPassword(password);
            }
        }
        user = new UserService().userLogin(user, isPhoneLogin);
//        System.out.println(user != null);
        return user != null ? user.getUserId() : String.valueOf(-1);
    }

    /**
     * User:返回个人用户信息/Admin:得到所有用户的信息列表
     * @param userid 用户userid
     * @return User:返回个人用户信息/Admin:得到所有用户的信息列表
     */
    public UserInfo getUserInfo(List<String> userid) {
        UserInformation user = new UserInformation();
        List<String> users = new ArrayList<String>();
//        if (userid == null || userid.length() <= 0) {
//            System.out.println("参数错误,请重新输入");
//        }else{
//            users.add(userid);
//        }
        try {
            return new UserService().getUserInfo(userid,false).get(0);
        }catch (Exception e) {
            return new UserInfo();
        }
    }

    /**
     * 用户注册功能方法实现
     * @param username 用户名
     * @param password 用户密码
     * @return 返回用户注册的功能
     */
    public String userCreated(String username, String password){
        UserInformation user = new UserInformation();
        user.setUserName(username);
        user.setUserPassword(password);
        user.setUserPhone(username);
        Integer userCreatedSuccessfully=new UserService().userCreateUserInformation(user);
//        System.out.println(userCreatedSuccessfully);
        if (userCreatedSuccessfully == -1) {
            return "注册失败,用户名或手机号重复";
        }else if(userCreatedSuccessfully == 1){
            return "注册成功,请前往注册";
        }else{
            return "服务器繁忙，请稍后再试";
        }
    }

    /**
     * 用户注销功能实现方法
     * @param userid 用户id
     * @param userDelete 用户删除/注销标记
     * @return 返回用户注销的功能
     */
    public String userUpdate(String userid,Boolean userDelete){
        UserExtendedInformation user = new UserExtendedInformation();
        List<String> userList = new ArrayList<String>();
        user.setUserId(userid);
        user.setUserDelete(userDelete);
        userList.add(user.getUserId());
        Integer userUpdatedSuccessfully=new UserService().userUpdateUserInformation(userList,user);
        if (userUpdatedSuccessfully == -1) {
            return "修改失败,请稍后再试";
        }else if(userUpdatedSuccessfully == 1){
            return "修改成功";
        }else{
            return "服务器繁忙，请稍后再试";
        }
    }

    /**
     * 用户密码修改功能实现方法
     * @param userid 用户id
     * @param userDelete 用户的新密码
     * @return 返回用户注销的功能
     */
    public String userPasswordUpdate(String userid,String userDelete){
        UserInformation user = new UserInformation();
        user.setUserId(userid);
        user.setUserPassword(userDelete);
        Integer userUpdatedSuccessfully=new UserService().userUpdateUserInformation(user);
        if (userUpdatedSuccessfully == -1) {
            return "修改失败,请稍后再试";
        }else if(userUpdatedSuccessfully == 1){
            return "修改成功";
        }else{
            return "服务器繁忙，请稍后再试";
        }
    }

    /**
     * 用户发送手机号
     * @param userPhone 用户手机号
     * @return 返回是否成功信息
     */
    public String userPhone(String userPhone){
        PhoneCodeService phoneCodeService = new PhoneCodeService();
        String phoneCode = phoneCodeService.setPhoneCode(userPhone);
        if ("-1".equals(phoneCode)) {
            return "发送失败,请稍后再试";
        }else if ("1".equals(phoneCode)) {
            return "发送成功，请前往查看";
        }else{
            return "服务器繁忙，请稍后再试";
        }
    }

    /**
     * 验证码验证
     * @param userPhone 用户手机号
     * @param userCode 用户验证码
     * @return 返回是否验证通过
     */
    public boolean userPhone(String userPhone,String userCode){
        PhoneCodeService phoneCodeService = new PhoneCodeService();
        return phoneCodeService.getPhoneCode(userPhone,userCode);
    }
}
